import 'package:flutter/material.dart';
import 'package:library_base/constant/constant.dart';
import 'package:library_base/entities/in_order_page_goods_details_entity.dart';
import 'package:library_base/ext/bool_ext.dart';
import 'package:library_base/ext/double_ext.dart';
import 'package:library_base/ext/list_ext.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:library_base/res/decoration.dart';
import 'package:library_base/res/styles.dart';
import 'package:library_base/res/widget_generator.dart';
import 'package:library_base/utils/sp_util.dart';
import 'package:library_base/utils/theme_util.dart';
import 'package:library_base/ext/theme_data_ext.dart';
import 'package:library_base/res/edge.dart';
import 'package:get/get.dart';
import 'package:library_base/widget/button/gradient_button.dart';
import 'package:library_base/widget/button/ink_bg_well.dart';
import 'package:library_base/widget/image/local_image.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

Widget batchEditWell(
    Function() entering,
    ) {
  return inkBgWell(
    child: Container(
        padding: EdgeInsets.symmetric(horizontal: 24.w, vertical: 24.w),
        child: LocalImage('common_edit',
            package: Constant.packLib(), width: 38.w, height: 38.w)),
    onTap: () {
      entering();
    },
    splashColor: ThemeUtil.getTheme().colorScheme.primary.withOpacity(0.5),
  );
}

Widget buildBottom(
    bool enable,
  double totalWeight, {
  void Function()? save,
}) {
  return Container(
    color: Colors.white,
    child: Column(
      children: [
        Container(
            height: 166.w,
            decoration: rectTop,
            alignment: Alignment.topRight,
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Expanded(
                    child: Container(
                  color: Colors.transparent,
                  child: Stack(
                    children: [
                      Positioned(
                        top: 47.w,
                        left: 24.w,
                        child: Row(
                          children: [
                            Text(
                              '${MultiLanguage.totalWeight.tr} : $totalWeight ${SPUtil.isJin.unitStrZhWithBracket()}',
                              style: normalTextColor28,
                            )
                          ],
                        ),
                      ),
                    ],
                  ),
                )),
                GradientButton(
                  ThemeUtil.getTheme(),
                  height: 77.w,
                  width: 184.w,
                  margin: EdgeInsets.only(right: 24.w, top: 21.w),
                  padding: EdgeInsets.symmetric(horizontal: 30.w),
                  text: MultiLanguage.save.tr,
                  onPressed: enable ? save : null,
                ),
              ],
            )),
      ],
    ),
  );
}

Widget buildHead() {
  List<String> headTitles = [
    MultiLanguage.serialNumber.tr,
    MultiLanguage.barcode.tr,
    "${MultiLanguage.weightCatties.tr}${SPUtil.isJin.unitStrZhWithBracket()}",
  ];
  List<int> flexes = [1, 2, 2];

  List<Widget> widgets = [];

  flexes.forEachIndexed((index, action) {
    widgets.add(Expanded(
      flex: flexes[index],
      child: Container(
        alignment: Alignment.center,
        child: Text(
          headTitles[index],
          style: normalTextColor28,
          textAlign: TextAlign.center,
        ),
      ),
    ));
  });

  return Container(
    color: ThemeUtil.getTheme().secondaryBgColor(),
    padding: EdgeInsets.symmetric(vertical: 24.w),
    child: Row(
      children: widgets,
    ),
  );
}

List<Widget> buildItem(
  List<InOrderPageGoodsDetailsEntity> list,
  List<TextEditingController> weightControllers,
  List<FocusNode> weightFocusNodes,
  Function(String) weightChange,
) {
  List<Widget> widgets = [];
  for (int i = 0; i < list.length; i++) {
    widgets.add(Padding(
      padding: EdgeInsets.only(top: 24.w),
      child: Row(
        children: [
          Expanded(
              flex: 1,
              child: Text(
                '${i + 1}',
                textAlign: TextAlign.center,
                style: TextStyles.textNormal28,
              )),
          Expanded(
              flex: 2,
              child: Text(
                list[i].goodsBarCode,
                textAlign: TextAlign.center,
                style: TextStyles.textNormal28,
              )),
          Expanded(
              flex: 2,
              child: Container(
                color: Colors.transparent,
                padding: edgeL18R18,
                child: buildInput(
                    padding: EdgeInsets.symmetric(horizontal: 15.w, vertical: 15.w),
                    controller: weightControllers[i],focusNode: weightFocusNodes[i], onChanged: weightChange),
              )),
        ],
      ),
    ));
  }
  return widgets;
}

